package com.m1905.lib.download.listener;


import com.m1905.lib.download.db.DBManager;
import com.m1905.lib.download.model.DownloadDBItem;
import com.m1905.lib.download.model.State;
import com.m1905.lib.download.util.DLog;

//用来更新数据库和统一回调
public class DatabaseUpdateCallBack implements RequestTaskListener {

    protected DownloadDBItem downloadItem;

    private final DownloadStatusListener callBack;


    public DatabaseUpdateCallBack(DownloadDBItem downloadItem, DownloadStatusListener callBack) {
        this.downloadItem = downloadItem;
        this.callBack = callBack;
    }

    @Override
    public void onWaiting(boolean insert) {
        DLog.e("UnionRequestCallBack onWaiting: " + insert + "  " + downloadItem);
        if (downloadItem.getState() != State.WAITING) {
            downloadItem.setState(State.WAITING);
            downloadItem.setErrorCode(0);
            downloadItem.setErrorMsg("");
            downloadItem.setDownloadSpeed(0);
            DBManager.updateStateToDatabase(downloadItem);
        }
        if (callBack != null) {
            callBack.onWaiting(downloadItem, insert);
        }
    }

    @Override
    public void onStart() {
        downloadItem.setState(State.STARTED);
        downloadItem.setErrorCode(0);
        downloadItem.setErrorMsg("");
        downloadItem.setDownloadSpeed(0);
        DBManager.updateStateToDatabase(downloadItem);
        if (callBack != null) {
            callBack.onStart(downloadItem);
        }
        DLog.e("UnionRequestCallBack onStart: " + downloadItem);
    }

    @Override
    public void onFileSize(long total) {
        if (downloadItem.getTotalSize() == 0) {
            downloadItem.setTotalSize(total);
        }

        if (callBack != null) {
            callBack.onFileSize(downloadItem);
        }
    }

    @Override
    public void onLoading(long total, long current, long speed) {
        if (downloadItem.getState() != State.LOADING) {
            downloadItem.setState(State.LOADING);
            DBManager.updateStateToDatabase(downloadItem);
        }
        downloadItem.setDownloadSize(current);
        downloadItem.setDownloadSpeed(speed);
        DBManager.updateProgressToDatabase(downloadItem);
        DLog.e("UnionRequestCallBack onLoading: " + downloadItem);
        if (callBack != null) {
            callBack.onLoading(downloadItem);
        }
    }

    @Override
    public void onSuccess() {
        downloadItem.setState(State.SUCCESS);
        downloadItem.setDownloadSpeed(0);
        DBManager.updateStateToDatabase(downloadItem);
        DLog.e("UnionRequestCallBack onSuccess: " + downloadItem);
        if (callBack != null) {
            callBack.onSuccess(downloadItem);
        }
    }

    @Override
    public void onFailure(int code, String message) {
        downloadItem.setState(State.FAILURE);
        downloadItem.setErrorCode(code);
        downloadItem.setErrorMsg(message);
        downloadItem.setDownloadSpeed(0);
        DBManager.updateStateToDatabase(downloadItem);
        DLog.e("UnionRequestCallBack onFailure: " + downloadItem + " code:" + code + "  ,  msg:" + message);
        if (callBack != null) {
            callBack.onFailure(downloadItem);
        }
    }

    @Override
    public void onCancel() {
        downloadItem.setState(State.CANCELLED);
        downloadItem.setDownloadSpeed(0);
        DBManager.updateStateToDatabase(downloadItem);
        DLog.e("UnionRequestCallBack onCancelled: " + downloadItem);
        if (callBack != null) {
            callBack.onCancel(downloadItem);
        }
    }

    @Override
    public void onDelete() {
        DBManager.deleteDatabase(downloadItem);
        DLog.e("UnionRequestCallBack onDelete: " + downloadItem);
        if (callBack != null) {
            callBack.onDelete(downloadItem);
        }
    }
}